#include "camera.h"

CameraHandle::CameraHandle(irr::scene::ICameraSceneNode& camera, irr::f32 y,  irr::f32 p)
	: camera_reference(camera)
	, yaw(y)
	, pitch(p)
{
	camera.setInputReceiverEnabled(false);
	turn(yaw, pitch);
}

void CameraHandle::relativeVector(irr::core::vector3df &v)
{
	irr::core::matrix4 mx_rotation;
	mx_rotation.setRotationDegrees( irr::core::vector3df(pitch, yaw, 0) );
	mx_rotation.rotateVect(v);
}

void CameraHandle::moveLF(irr::f32 l, irr::f32 f)
{
	irr::core::matrix4   mx_rotation;
	irr::core::vector3df v_forward (0, 0, -CAMERAMOVESPEED * f);
	irr::core::vector3df v_left    (CAMERAMOVESPEED * l, 0, 0);

	mx_rotation.setRotationDegrees( irr::core::vector3df(pitch, yaw, 0) );
	mx_rotation.rotateVect(v_forward);
	mx_rotation.rotateVect(v_left);

	camera_reference.setPosition( camera_reference.getPosition() + v_left + v_forward );
}

void CameraHandle::moveLU(irr::f32 l, irr::f32 u)
{
	irr::core::matrix4   mx_rotation;
	irr::core::vector3df v_upward (0, -CAMERAMOVESPEED * u, 0);
	irr::core::vector3df v_left   (CAMERAMOVESPEED * l, 0, 0);

	mx_rotation.setRotationDegrees( irr::core::vector3df(pitch, yaw, 0) );
	mx_rotation.rotateVect(v_left);
	mx_rotation.rotateVect(v_upward);

	camera_reference.setPosition( camera_reference.getPosition() + v_left + v_upward );
}

void CameraHandle::turn(irr::f32 d_yaw, irr::f32 d_pitch)
{
	yaw   += d_yaw;
	pitch += d_pitch;

	irr::core::matrix4   mx_rotation;
	irr::core::vector3df v_forward (0, 0, 100000);
	irr::core::vector3df v_up      (0, 1, 0);

	mx_rotation.setRotationDegrees( irr::core::vector3df(pitch, yaw, 0) );
	relativeVector(v_forward);
	relativeVector(v_up);

	camera_reference.setTarget   ( camera_reference.getPosition() + v_forward );
	camera_reference.setUpVector ( v_up );
}